package com.spyx.acl.service.impl;

import com.atyx.spyx.model.acl.Permission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spyx.acl.mapper.PermissionMapper;
import com.spyx.acl.service.PermissionService;
import com.spyx.acl.util.MyPermissionHelper;
import com.sun.javafx.tk.PermissionHelper;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
implements PermissionService {


    @Override   //如果是父级菜单的id,需要将子菜单的所有id全部删除
    public void deletebyids(Integer id) {
        //1.创建一个集合，用来保存被删除的菜单id
        List ids=new ArrayList();
        ids.add(id);
        //2.根据被删除的id值，查找该id所包含的子菜单id
        findNextMenuIds(id,ids);
        //3.执行批量删除
        baseMapper.deleteBatchIds(ids);

    }

    public void findNextMenuIds(Integer id,List<Integer> ids){
        //查询菜单表中pid等于被删除的id
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<Permission>();
        wrapper.eq(Permission::getPid,id);
        List<Permission> permissionList = baseMapper.selectList(wrapper);
        //遍历集合，取出集合中的菜单id即可
        for (Permission p1 : permissionList) {
            ids.add(p1.getId().intValue());
            //继续查询p1的子菜单id集合
            findNextMenuIds(p1.getId().intValue(),ids);
        }
    }


    @Override
    public List<Permission> queryMenus() {
        //1.获取所有菜单-未分级
        List<Permission> permissionList = baseMapper.selectList(null);
        //2.将未分级的菜单实现分级功能
        List<Permission> lastMenus= MyPermissionHelper.build(permissionList);

        return lastMenus;
    }
}
